package com.gateway.app;

import com.gateway.app.lb.CgRequestRateLimiterGatewayFilterFactory;
import com.gateway.app.lb.DayRequestRateLimiterGatewayFilterFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.gateway.filter.factory.GatewayFilterFactory;
import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.cloud.gateway.filter.ratelimit.RateLimiter;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;
import reactor.core.publisher.Mono;

import java.net.InetAddress;
import java.net.UnknownHostException;

@Slf4j
@SpringBootApplication
public class SpringcloudGatewayApplication {
 
    public static void main(String[] args) throws UnknownHostException {

        log.info("========微服务启动开始========");
        ConfigurableApplicationContext application = SpringApplication.run(SpringcloudGatewayApplication.class, args);
        Environment env = application.getEnvironment();
        String ip = InetAddress.getLocalHost().getHostAddress();
        String port = env.getProperty("server.port");
        String path = env.getProperty("server.servlet.context-path") == null ? "" : env.getProperty("server.servlet.context-path");
        log.info("\n----------------------------------------------------------\n\t" +
                "Application is running! Access URLs:\n\t" +
                "Local: \t\thttp://localhost:" + port + path + "/\n\t" +
                "External: \thttp://" + ip + ":" + port + path + "/\n\t" +
                "Swagger文档: \thttp://" + ip + ":" + port + path + "/doc.html\n" +
                "----------------------------------------------------------");
        log.info("========微服务启动成功========");

    }
    
    /*
     * curl http://localhost:8001/clearIp
     * curl http://localhost:8001/putIp?ips=43.133.81.217:7860
     * curl http://localhost:8001/checkpreload.htm
     *
     *
     */


    @Primary
    @Bean(value = "ipKeyResolver")
    KeyResolver ipKeyResolver() {
        return exchange -> {
            String ip = exchange.getRequest().getRemoteAddress().getAddress().getHostAddress();
            log.info("ip:{}",ip);
            String url = exchange.getRequest().getURI().getPath();
            log.info("url:{}",url);
            ip = url;
            return Mono.just(ip);
        };
    }

    @Primary
    @Bean(value = "CgRequestRateLimiter")
    GatewayFilterFactory cgRequestRateLimiter(RateLimiter defaultRateLimiter, KeyResolver defaultKeyResolver){
        return new CgRequestRateLimiterGatewayFilterFactory(defaultRateLimiter,defaultKeyResolver);
    }

    @Primary
    @Bean(value = "DayRequestRateLimiter")
    GatewayFilterFactory dayRequestRateLimiter(){
        return new DayRequestRateLimiterGatewayFilterFactory();
    }
}